//
//  LoginViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/10/31.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "LoginViewController.h"
#import "zhuceViewController.h"
#import "UserSingleton.h"
#import "UserLogin.h"
@interface LoginViewController ()


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.loginView = [[LoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.loginView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.loginView];
    [_loginView release];
    
    self.navigationItem.title = @"用户登陆";
    
    
    [self.loginView.longinButton addTarget:self action:@selector(longinButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.loginView.zhuceButton addTarget:self action:@selector(zhuceButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    

}

- (void)rightAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)longinButtonAction:(UIButton *)but1
{
    NSUserDefaults *userDefanlt = [NSUserDefaults standardUserDefaults];
    if (but1.selected == NO) {
        [userDefanlt setObject:self.loginView.userTextField.text forKey:@"name"];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"name"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
//    but1.selected = !but1.selected;
    
    [[UserSingleton shareSingleton] openUB];
    [[UserSingleton shareSingleton] createTable];
    NSMutableArray *array = [NSMutableArray array];
    array = [[UserSingleton shareSingleton] selectUser:self.loginView.userTextField.text];
//    NSLog(@"array ====== %@", array);
    
    if ([self.loginView.userTextField.text isEqualToString:@""]) {
        UIAlertView *alerView0 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不能为空" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alerView0 show];
    }
    
    if (array.count == 0) {
        UIAlertView *alerView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"用户名不存在请注册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alerView1 show];
    }
    UserLogin *user = [[UserLogin alloc] init];
    user = [array objectAtIndex:0];
    if (([self.loginView.userTextField.text isEqualToString:user.uname])&& ([self.loginView.codeTextField.text isEqualToString:user.code])) {
        [self dismissViewControllerAnimated:YES completion:^{
            
            NSLog(@"%@",user.uname);
        }];
    }else{
        UIAlertView *alerVier2 = [[UIAlertView alloc] initWithTitle:@"用户名密码不正确" message:@"请核对信息是否正确" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alerVier2 show];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:self.loginView.userTextField.text forKey:@"name"];
    
   
}

- (void)zhuceButtonAction:(UIButton *)but2
{
    
    zhuceViewController *zhuceVC = [[zhuceViewController alloc] init];
    [self.navigationController pushViewController:zhuceVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
